package com.pl.itsense.gwt.clientzone.server;

import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.pl.itsense.gwt.clientzone.client.Clientzone;
import com.pl.itsense.gwt.clientzone.model.EventType;
import com.pl.itsense.gwt.clientzone.model.Member;

public class RegistrationServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(RegistrationServlet.class.getName());

	private static final long serialVersionUID = 1L;

	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		  EntityManager em = EMF.get().createEntityManager();
		  String hash = req.getParameter("hash");
		  String id  = req.getParameter("id");
		  if ((hash == null) || (id == null)) return;
		  try{
			  Member member = em.find(Member.class, new Long(id));
			  if ( (member != null) && (member.getRegistrationHash().compareTo(hash) == 0 ) ){
				  member.setActive(true);
				  EntityTransaction trx = em.getTransaction();
				  trx.begin();
				  em.persist(member);
				  trx.commit();
				  resp.sendRedirect("http://www.itsense.com.pl/aktywacjaKonta.html");
			  }else{
				  log.severe("Blad podczas aktywacji konta, Member == null LUB hash sie nie zgadza");
			  }
		  }catch(Exception ex){
			  log.severe("Blad podczas aktywacji konta " + ex.getMessage() + " " + ex.toString());
		  }
	}
	
	
	
	
}
